package com.sicheng.algorithm.recursive;

import java.util.Arrays;

/**
 * @author : zsc
 */
public class QueenN {
    private static final int MAX = 8;
    private static final int[] queen = new int[MAX];
    private static int total;

    public static void main(String[] args) {
        QueenN.push();
        System.out.println("total=" + total);
    }

    public static void push() {
        push(0);
    }

    // 递归放置第n+1个皇后,n=0,1,2...
    public static void push(int n) {
        if (n == MAX) {   //此时N个皇后全部归位
            total++;
            System.out.println(Arrays.toString(queen));
            return;
        }
        for (int i = 0; i < MAX; i++) {
            queen[n] = i;
            if (!isConflicted(n)) {
                push(n + 1);
            }

        }
    }

    // 检测是否发送冲突
    public static boolean isConflicted(int n) {
        for (int i = 0; i < n; i++) {
            if (queen[i] == queen[n] || Math.abs(n - i) == Math.abs(queen[i] - queen[n]))
                return true;
        }
        return false;
    }
}
